package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.exception.SaveException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.FilterGroupOption;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.InformDto;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.base.entity.Dept;
import com.mrd.gtimp.base.entity.Inform;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.base.service.CompanyBiz;
import com.mrd.gtimp.base.service.InformBiz;
import com.mrd.gtimp.base.service.UserBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */

@Controller
@RequestMapping("/base")
public class InformAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private InformBiz informBiz;

    @Resource
    private CompanyBiz companyBiz;

    @Resource
    private UserBiz userBiz;


    /**
     * public String show() { try { page = initPage(InformBiz.findEntityCount(filters)); Order order
     * = new Order(); order.add(sidx, sord); Informs = InformBiz .findEntityByFilter(page.getCurrentPageStartNo(),
     * page.getPageSize(), filters, order); } catch (Exception e) { e.printStackTrace(); } return
     * SUCCESS; }
     **/

    @RequestMapping("/inform-mainCompany")
    public String mainCompany() {
        return "/gtimp/base/inform/companyList";
    }

    /**
     * 转去通知信息列表
     */
    @RequestMapping("/inform-show")
    public String show() {
        return "/gtimp/base/inform/informList";
    }

    /**
     * 显示通知信息列表
     */
    @RequestMapping("/inform-informList")
    @ResponseBody
    public Page<InformDto> informList(User user, Jqgrid jqgrid) {
        try {
            List<InformDto> informDtos=null;
//            page = initPage(informBiz.findEntityCount());
            Page<Inform> informs = informBiz.findByJqgrid(jqgrid);
            if (informs != null && informs.getSize() > 0) {
                informDtos = new ArrayList<InformDto>();
                for (Inform f : informs) {
                    user = userBiz.findEntityById(f.getUserid());
                    InformDto dto = new InformDto();
                    dto.setContents(f.getContents());
                    dto.setId(f.getId());
                    dto.setIssend(f.getIssend());
                    dto.setIssuccess(f.getIssuccess());
                    dto.setSendtime(f.getSendtime());
                    dto.setTypes(f.getTypes());
                    dto.setUmobile(user.getMobile());
                    dto.setUname(user.getName());
                    dto.setUserid(f.getUserid());
                    informDtos.add(dto);
                }
            }
            Page<InformDto> InformDtoss=new PageImpl<InformDto>(informDtos);
            return  InformDtoss;
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 公司列表
     */
    @RequestMapping("/inform-listCompany")
    @ResponseBody
    public Page<Company> listCompany() {
        try {
            Filter filters=new Filter();
            if (null == filters) {
                filters = new Filter();
                filters.setGroupOp(FilterGroupOption.AND);
            }
            SearchRule rule = new SearchRule("isabled", SearchRuleOption.eq, "0");
            SearchRule rule2 = new SearchRule("state", SearchRuleOption.eq, "0");
            filters.addRule(rule);
            filters.addRule(rule2);
             Pageable page = new PageRequest(0, 15, Sort.Direction.ASC );
            Jqgrid jqgrid=new Jqgrid(filters,page);
         Page<Company> companys=  companyBiz.findByJqgrid(jqgrid);
            for (Company com : companys) {
                com.setIsParent(true);
            }
            return companys;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/inform-getUserByDeptId")
    @ResponseBody
    public  Page<User> getUserByDeptId(Dept dept,Jqgrid jqgrid) {
        String deptId = "";
        try {
            if (StringUtils.isNotEmpty(dept.getId())) {
                deptId = dept.getId();
//                if (filters == null) {
//                    filters = new Filter();
//                    filters.setGroupOp(FilterGroupOption.AND);
//                }
                SearchRule sr = new SearchRule("depts.id", SearchRuleOption.eq, deptId);
                jqgrid.addRule(sr);
            }
//            page = initPage(userBiz.findEntityCount(filters));
            Page<User> users= userBiz.findByJqgrid(jqgrid);
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/inform-getUserByCompanyId")
    @ResponseBody
    public Page<User> getUserByCompanyId(Company company,Jqgrid jqgrid) {
        String companyId = "";
        try {
            if (StringUtils.isNotEmpty(company.getId())) {
                companyId = company.getId();
                SearchRule sr = new SearchRule("depts.company.id", SearchRuleOption.eq, companyId);
                jqgrid.addRule(sr);
            }
//            page = initPage(userBiz.findEntityCount(filters));
//            users = userBiz.findEntityByFilter(0, 0, filters, null);
            return userBiz.findByJqgrid(jqgrid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 保存通知信息
     */
    @RequestMapping("/inform-save")
    @ResponseBody
    public Result save(Inform inform,String userids) {
        Result msg = null;
        try {
            if (StringUtils.isNotEmpty(userids)) {
                String ids[] = userids.split(",");
                for (int i = 0; i < ids.length; i++) {
                    inform.setId(null);
                    inform.setIssend("0");
                    inform.setUserid(ids[i]);
                    informBiz.saveOrupdateEntity(inform);
                }
                msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
            }
        } catch (SaveException e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }


}
